package com.ct.ah.lab.controller;

import com.ct.ah.lab.entity.Point;
import com.ct.ah.lab.entity.PointItem;
import com.ct.ah.lab.entity.User;
import com.ct.ah.lab.entity.vo.PointItemVo;
import com.ct.ah.lab.service.IPointService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/points")
public class PointController {
    @Autowired
    private IPointService pointService;

    //查询用户当前积分
    @RequestMapping("/getByUid")
    @ResponseBody
    public Map<String, Object> getByUid(HttpServletRequest req) {
        Map<String, Object> map = new HashMap<>();

        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("loginUser");

        if (user == null) {
            map.put("msg", "not login");
        } else {
            Point point = pointService.getPoint(user.getUid());
            map.put("state", 1000);
            map.put("data", point);
            if (point != null) {
                map.put("msg", true);
            } else {
                map.put("msg", false);

            }
        }
        return map;
    }

    //根据用户id-查询所有的积分记录和订单记录
    @RequestMapping("/list")
    @ResponseBody
    public Map<String, Object> list(HttpServletRequest req) {
        Map<String, Object> map = new HashMap<>();
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("loginUser");
        if (user == null) {
            map.put("msg", "not login");
        } else {
            List<PointItemVo> pointItemVos = pointService.listPointItem(user);
            map.put("state", 1000);
            map.put("data", pointItemVos);
        }
        return map;
    }
    //根据用户id-查询所有的积分记录和订单记录
    @RequestMapping("/checkPointAdd")
    @ResponseBody
    public Map<String, Object> checkPointAdd(HttpServletRequest req) {
        Map<String, Object> map = new HashMap<>();
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("loginUser");
        if (user == null) {
            map.put("msg", "not login");
        } else {
            pointService.checkPointAdd(new Double(1),user.getUid());
            map.put("state", 1000);
        }
        return map;
    }

    //

    //根据一个订单保存一条积分记录
    @RequestMapping("/addPointByOrderId")
    @ResponseBody
    public Map<String, Object> addPointByOrderId(Integer oid, HttpServletRequest req) {
        Map<String, Object> map = new HashMap<>();
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("loginUser");
        if (user == null) {
            map.put("msg", "not login");
        } else {
            pointService.savePointItem(oid);
            map.put("state", 1000);
        }
        return map;
    }

    //使用积分进行支付
    @RequestMapping("/payByPoint")
    @ResponseBody
    public Map<String, Object> payByPoint(Integer oid, HttpServletRequest req) {
        Map<String, Object> map = new HashMap<>();
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("loginUser");
        if (user == null) {
            map.put("msg", "not login");
        } else {
            pointService.reducePoint(oid, user);
            map.put("state", 1000);
        }
        return map;
    }
}
